package com.allen.admin.service.server.manager.impl;

import com.allen.admin.service.common.domain.AdminUser;
import com.xxx.dao.GenericDao;
import com.xxx.page.DataPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AdminUserManager {
	
	@Autowired
    private GenericDao dao;
	
	public AdminUser insert(AdminUser obj) {
		assert obj != null;
		
		obj.setCreatedTime(LocalDateTime.now());
		obj.setUpdatedTime(LocalDateTime.now());
		int affected = dao.insertAndReturnAffectedCount("AdminUserMapper.insert", obj);
		if (affected > 0) {
			return obj;
		} else {
			return null;
		}
	}

	public AdminUser update(AdminUser obj) {
		assert obj != null;
		
		obj.setUpdatedTime(LocalDateTime.now());
		int affected = dao.updateByObj("AdminUserMapper.update", obj);
		
		if (affected > 0) {
			return this.select(obj.getId());
		} else {
			return null;
		}
	}
	
	private AdminUser select(Long id, String account, String password) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if (id != null && id > 0) {
			map.put("id", id);
		} else if (StringUtils.isNotBlank(account)) {
			map.put("account", account);
			
			if (StringUtils.isNotBlank(password)) {
				map.put("password", password);
			}
		}
		
		AdminUser obj = dao.queryUnique("AdminUserMapper.select", map);
		return obj;
	}

	public AdminUser select(Long id) {
		return this.select(id, null, null);
	}

	public AdminUser select(String account) {
		return this.select(null, account, null);
	}

	public AdminUser select(String account, String password) {
		return this.select(null, account, password);
	}

//	public List<AdminUser> queryByRole(String role) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("role", role);
//		return dao.queryList("AdminUserMapper.queryByRole", map);
//	}
	
	public List<AdminUser> select(List<Long> ids) {
		assert ids != null && !ids.isEmpty();
		
		Map<String, Object> map = new HashMap<String, Object>();
		if (ids.size() == 1) {
			map.put("id", ids.get(0));
		} else {
			map.put("ids", ids);
		}
		
		return dao.queryList("AdminUserMapper.selectByIds", map);
	}
	
	/**
	 * 
	 * @param departmentId required
	 * @param status optional
	 * @return
	 */
	public List<AdminUser> selectByDepartment(Integer departmentId, Integer status) {
		assert departmentId != null;
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("departmentId", departmentId);
		if (status != null) {
			map.put("status", status);
		}
		
		return dao.queryList("AdminUserMapper.selectByDepartment", map);
	}
	
	public DataPage<AdminUser> queryPage(DataPage<AdminUser> dataPage, AdminUser qObj) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("order", dataPage.getOrder());
		map.put("orderBy", dataPage.getOrderBy());
		
		if (StringUtils.isNotBlank(qObj.getAccount())) {
			map.put("account", "%" + qObj.getAccount() + "%");
		}
		if (StringUtils.isNotBlank(qObj.getName())) {
			map.put("name", "%" + qObj.getName() + "%");
		}
		if (qObj.getDepartmentId() != null) {
			map.put("departmentId", qObj.getDepartmentId());
		}
		if (qObj.getStatus() != null) {
			map.put("status", qObj.getStatus());
		}
		if (StringUtils.isNotBlank(qObj.getRoleIds())) {
			map.put("roleIds", qObj.getRoleIds());
		}
		
		dataPage = dao.queryPage(
				"AdminUserMapper.countPage", 
				"AdminUserMapper.queryPage", map, dataPage);
		
		return dataPage;
	}

	public List<AdminUser> selectByRoleId(Integer roleId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleId", roleId);
		return dao.queryList("AdminUserMapper.selectByRoleId", map);
	}
	
	public List<AdminUser> queryByAccounts(List<String> accounts){
		//避免后台进行整表查询
		if (accounts == null || accounts.size() ==0){
			return null;
		}

		Map<String, Object> map = new HashMap<String,Object>();
		if (accounts.size() == 0){
			map.put("account", accounts.get(0));
		} else {
			map.put("accounts", accounts);
		}
		return dao.queryList("AdminUserMapper.selectByaccounts", map);	
	}
}
